#!/usr/bin/env bash

# Test Java plugin URL tracking functionality
export MISE_LOCKFILE=1
export MISE_EXPERIMENTAL=1

# Clean up any existing installations
mise uninstall java@17.0.2 2>/dev/null || true

# Create initial configuration
cat <<EOF >mise.toml
[tools]
java = { version = "17.0.2" }
EOF

# Create lockfile
touch mise.lock

# First install - should fetch URL from Java metadata API
echo "=== First install - fetching URL from Java metadata API ==="
mise install
assert_contains "mise x java -- java -version 2>&1" 'openjdk version "17.0.2"'

# Verify URL is stored in lockfile
echo "=== Verifying URL is stored in lockfile ==="
assert_contains "cat mise.lock" '[tools.java]'
assert_contains "cat mise.lock" 'version = "17.0.2"'
assert_contains "cat mise.lock" 'backend = "core:java"'
assert_contains "cat mise.lock" 'url ='

# Uninstall and reinstall - should reuse URL from lockfile
echo "=== Reinstalling - should reuse URL from lockfile ==="
mise uninstall java@17.0.2
mise install
assert_contains "mise x java -- java -version 2>&1" 'openjdk version "17.0.2"'

# Verify the tool still works after reinstall
echo "=== Verifying tool still works after reinstall ==="
assert_contains "mise x java -- java -version 2>&1" 'openjdk version "17.0.2"'

echo "=== Java URL tracking test completed successfully ==="
